﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendlyURL.Web.UI.Models;
using FriendlyURL.Web.Domain.Impl;
using FriendlyURL.Web.Domain.Abstract;

namespace FriendlyURL.Web.UI.Controllers
{
    public class AliasController : Controller
    {
        private readonly ILinkRepository _linkRepository;
        public AliasController(ILinkRepository linkRepository)
        {
            _linkRepository = linkRepository;
        }
        //
        // GET: /Alias/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Alias/Details/5

        public ActionResult Details(string aliasName)
        {
            return View();
        }

        //
        // GET: /Alias/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Alias/Create

        [HttpPost]
        public ActionResult Create(CreateAliasModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to create the alias
                AliasCreateStatus createStatus;
                _linkRepository.CreateLink(model.ShortURL, model.DestinationURL, model.Description, DateTime.Now, 21, DateTime.Now, 21, 21, true, out createStatus);

                if (createStatus == AliasCreateStatus.Success)
                {
                    return RedirectToAction("CreateAliasSuccess");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/CreateAliasSuccess

        public ActionResult CreateAliasSuccess()
        {
            return View();
        }
        
        //
        // GET: /Alias/Edit/5

        public ActionResult Edit(string aliasName)
        {
            return View();
        }

        //
        // POST: /Alias/Edit/5

        [HttpPost]
        public ActionResult Edit(string aliasName, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Alias/Delete/5

        public ActionResult Delete(string aliasName)
        {
            return View();
        }

        //
        // POST: /Alias/Delete/5

        [HttpPost]
        public ActionResult Delete(string aliasName, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult RedirectAlias(string alias)
        {
            
            try
            {
                var redirectLink = _linkRepository.GetLinkByShortURL(alias);

                if (redirectLink != null)
                {
                    return RedirectPermanent(redirectLink.LongUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            catch(Exception ex)
            {
                ErrorViewModel evm = new ErrorViewModel();
                evm.ActionName = "Redirect";
                evm.ControllerName = "Alias";
                evm.Exception = ex;
                evm.FriendlyErrorMessage = "An error occured while trying to redirect to the alias";
                return View("Error", evm);
            }
        }

        #region Status Codes
        private static string ErrorCodeToString(AliasCreateStatus createStatus)
        {
            switch (createStatus)
            {
                case AliasCreateStatus.DuplicateAliasName:
                    return "Alias already exists. Please enter a different alias.";

                case AliasCreateStatus.DuplicateDestinationURL:
                    return "An alias for that destination url already exists. Please enter a different destination url.";

                case AliasCreateStatus.Error:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion
    }
}
